package com.wn.cloud.consumer.controller;

import com.wn.cloud.common.entity.Payment;
import com.wn.cloud.common.response.CommonResult;
import com.wn.cloud.common.response.DataResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @FileName: OrderController
 * @Description: TODO
 * @Author: MaWenHao
 * @Created: 2020/3/8 - 14:59
 * @Version: V1.0
 * @Company: 东方微银科技（北京）有限公司
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    private static final String PAYMENT_URL = "http://cloud-payment-service";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/{id}")
    public CommonResult getPayment(@PathVariable("id") long id) {
        DataResult<Payment> dataResult = restTemplate.getForObject(PAYMENT_URL + "/payment/" + id, DataResult.class);
        return dataResult;
    }

    @PostMapping
    public CommonResult createPayment(@RequestBody Payment payment) {
        return restTemplate.postForObject(PAYMENT_URL + "/payment", payment, CommonResult.class);
    }

    @GetMapping("/entity/{id}")
    public ResponseEntity<DataResult> getEntityPayment(@PathVariable("id") long id) {
        ResponseEntity<DataResult> dataResult = restTemplate.getForEntity(PAYMENT_URL + "/payment/" + id, DataResult.class);
        return dataResult;
    }
}
